package com.example.foodsy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.foodsy.dto.AddressDTO;
import com.example.foodsy.entity.City;
import com.example.foodsy.entity.CompanyInfo;
import com.example.foodsy.entity.FarmInfo;
import com.example.foodsy.entity.Province;
import com.example.foodsy.mapper.CityMapper;
import com.example.foodsy.mapper.CompanyInfoMapper;
import com.example.foodsy.mapper.FarmInfoMapper;
import com.example.foodsy.mapper.ProvinceMapper;
import com.example.foodsy.service.CityService;
import com.example.foodsy.service.FarmInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 养殖企业产品批号信息表 服务实现类
 * </p>
 *
 * @author cgc
 * @since 2025-09-23
 */
@Service
public class FarmInfoServiceImpl extends ServiceImpl<FarmInfoMapper, FarmInfo> implements FarmInfoService {

	@Autowired
	private CityMapper cityMapper;
	@Autowired
	private ProvinceMapper provinceMapper;
	@Autowired
	private CompanyInfoMapper companyInfoMapper;

	@Override
	public FarmInfo getByBatchId(String batchId) {
		FarmInfo farmInfo = baseMapper.selectOne(new QueryWrapper<FarmInfo>().eq("batch_id", batchId));
		return farmInfo;
	}



	@Override
	public boolean editFarmInfo(FarmInfo farmInfo) {
		if (farmInfo.getFbId() != null) {
			return updateById(farmInfo);
		}
		return false;
	}


	//新建产品信息
	@Override
	public boolean saveFarmInfo(FarmInfo farmInfo) {
		//设置导入日期
		farmInfo.setFmDate(LocalDate.now());
		//设置待发布状态
		farmInfo.setState(1);

		//新建
		return save(farmInfo);
	}

	//查询所有产品批号
	@Override
	public List<String> getAllBatchId( ) {
		List<FarmInfo> farmInfos = baseMapper.selectList(null);
		List<String> batchIds = farmInfos.stream().map(FarmInfo::getBatchId).toList();
		return batchIds;
	}

	@Override
	public AddressDTO<FarmInfo> getInfoByFbId(String fbId) {
		AddressDTO<FarmInfo> addressDTO = new AddressDTO<>();
		//根据fbId获取farminfo
		FarmInfo farmInfo = baseMapper.selectOne(new QueryWrapper<FarmInfo>().eq("fb_id", fbId));
		//获取企业编号
		int farmId = farmInfo.getFarmId();
		//根据企业编号获取企业信息
		CompanyInfo companyInfo = companyInfoMapper.selectOne(new QueryWrapper<CompanyInfo>().eq("id", farmId));

		//根据fbId获取产品批号
		String batchId = farmInfo.getBatchId();

		Integer cityId = companyInfo.getCityId();
		Integer provId = companyInfo.getProvId();


		//查询对应的省份和城市
		City city = cityMapper.selectOne(new QueryWrapper<City>().eq("city_id", cityId));
		Province province = provinceMapper.selectOne(new QueryWrapper<Province>().eq("prov_id", provId));

		//添加属性值
		addressDTO.setCity(city.getCityName());
		addressDTO.setProvince(province.getProvName());
		addressDTO.setProvinceId(provId);
		addressDTO.setCityId(cityId);
		addressDTO.setCompanyName(companyInfo.getName());
		addressDTO.setCompanyInfo(farmInfo);
		addressDTO.setCompanyId(companyInfo.getId());
		return addressDTO;
	}
}
